| Conditions | 11 |
| Total Lines | 72 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like FairCalendarController.get often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import { |
||
| 35 | |||
| 36 | @Get() |
||
| 37 | @WithName('faircalendar_index') |
||
| 38 | @Render('pages/faircalendar/index.njk') |
||
| 39 | public async get( |
||
| 40 | @Query() dto: FairCalendarControllerDTO, |
||
| 41 | @LoggedUser() user: User |
||
| 42 | ) { |
||
| 43 | const date = |
||
| 44 | dto.month !== undefined && dto.year !== undefined |
||
| 45 | ? new Date(dto.year, dto.month, 15) |
||
| 46 | : new Date(); |
||
| 47 | |||
| 48 | const userId = dto.userId ? dto.userId : user['id']; |
||
| 49 | |||
| 50 | const users: UserView[] = await this.queryBus.execute(new GetUsersQuery()); |
||
| 51 | |||
| 52 | const events: FairCalendarView[] = await this.queryBus.execute( |
||
| 53 | new GetMonthlyFairCalendarQuery(date, userId) |
||
| 54 | ); |
||
| 55 | |||
| 56 | const overview = await this.overviewFactory.create(events); |
||
| 57 | const overviewTable = this.overviewTableFactory.create(overview); |
||
| 58 | |||
| 59 | const fullCalendarEvents = events.map(event => { |
||
| 60 | let title = `${minutesToHours(event.time)} - `; |
||
| 61 | |||
| 62 | const fcEventType = event.type.startsWith('leave_') |
||
| 63 | ? 'leave' |
||
| 64 | : event.type; |
||
| 65 | |||
| 66 | if (fcEventType === 'mission' && event.task && event.project) { |
||
| 67 | title += `${event.project.name} (${event.task.name})`; |
||
| 68 | } else if (fcEventType === 'leave') { |
||
| 69 | title += `${this.translator.translate('leaves-type-value', { |
||
| 70 | type: event.type.slice(6) |
||
| 71 | })}`; |
||
| 72 | } else { |
||
| 73 | title += `${this.translator.translate('faircalendar-type-option', { |
||
| 74 | type: event.type |
||
| 75 | })}`; |
||
| 76 | } |
||
| 77 | |||
| 78 | return { |
||
| 79 | // See: https://fullcalendar.io/docs/event-object |
||
| 80 | type: fcEventType, |
||
| 81 | start: event.date, |
||
| 82 | end: event.date, |
||
| 83 | title, |
||
| 84 | ...(event.id |
||
| 85 | ? { url: `/app/faircalendar/events/edit/${event.id}` } |
||
| 86 | : {}), |
||
| 87 | textColor: `var(--event-${fcEventType}-text)`, |
||
| 88 | backgroundColor: `var(--event-${fcEventType}-background)`, |
||
| 89 | borderColor: `var(--event-${fcEventType}-border)` |
||
| 90 | }; |
||
| 91 | }); |
||
| 92 | |||
| 93 | const currentYear = date.getFullYear(); |
||
| 94 | const minYear = dto.minYear ?? currentYear - 5; |
||
| 95 | const maxYear = dto.maxYear ?? currentYear; |
||
| 96 | |||
| 97 | return { |
||
| 98 | users, |
||
| 99 | overviewTable, |
||
| 100 | fullCalendarEvents, |
||
| 101 | date, |
||
| 102 | currentMonth: date.getMonth(), |
||
| 103 | currentYear: date.getFullYear(), |
||
| 104 | minYear, |
||
| 105 | maxYear, |
||
| 106 | userId |
||
| 107 | }; |
||
| 110 |